package patrol.mobile.com.mobilepatrol.activity.toll;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.dl7.recycler.listener.OnRecyclerViewItemClickListener;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import okhttp3.Call;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.BaseActivity;
import patrol.mobile.com.mobilepatrol.activity.toll.add_data.AddDataPage1Activity;
import patrol.mobile.com.mobilepatrol.adapter.JobEvaluationAdapter;
import patrol.mobile.com.mobilepatrol.api.Urls;
import patrol.mobile.com.mobilepatrol.bean.BanciBean;
import patrol.mobile.com.mobilepatrol.bean.DateBean;
import patrol.mobile.com.mobilepatrol.bean.JobEvaluationBean;
import patrol.mobile.com.mobilepatrol.bean.OrgListBean;
import patrol.mobile.com.mobilepatrol.manager.UserManager;
import patrol.mobile.com.mobilepatrol.utils.CommonUtil;
import patrol.mobile.com.mobilepatrol.utils.DatePickerUtil;
import patrol.mobile.com.mobilepatrol.utils.GsonUtil;
import patrol.mobile.com.mobilepatrol.utils.TimeUtils;
import patrol.mobile.com.mobilepatrol.utils.net.HttpCallBack;
import patrol.mobile.com.mobilepatrol.utils.net.HttpSender;
import patrol.mobile.com.mobilepatrol.view.CallBack;
import patrol.mobile.com.mobilepatrol.widget.MySpinner;

/**
 * 收费岗位考核
 */
public class JobEvaluationActivity extends BaseActivity implements CallBack<DateBean> {

    @BindView(R.id.mySpinner01)
    MySpinner mySpinner01;
    @BindView(R.id.mySpinner03)
    MySpinner mySpinner03;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.tv_start)
    TextView tvStart;
    private JobEvaluationAdapter mAdapter;
    private String currentOrgId = "";
    private String currentTime = "";
    private List<JobEvaluationBean> jobEvaluationBeans;
    private String period = "";

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_job_evaluation;
    }

    @Override
    public void initView() {
        super.initView();
        iv_back.setImageResource(R.mipmap.up_white);
        iv_back.setVisibility(View.VISIBLE);
        iv_back.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        CommonUtil.exitActivityAndBackAnim(JobEvaluationActivity.this, true);
                    }
                });
        tv_center_title.setVisibility(View.VISIBLE);
        tv_center_title.setText("岗位考核");
        iv_right_title.setVisibility(View.VISIBLE);
        iv_right_title.setImageResource(R.mipmap.add_white);
        tvStart.setText(TimeUtils.dateString(System.currentTimeMillis()));
       /* iv_right_title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(JobEvaluationInformationActivity.class);
            }
        });*/
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new JobEvaluationAdapter(this);
        recyclerView.setAdapter(mAdapter);
//        final List<String> threeData = new ArrayList<>();
//        threeData.add("第1班次");
//        threeData.add("第2班次");
//        threeData.add("第3班次");
//        threeData.add("第4班次");
//        mySpinner03.setmDatas(threeData, new MySpinner.CallBack() {
//            @Override
//            public void onSelect(int postion) {
//                period = threeData.get(postion);
//                requestData(UserManager.getOrgNo(), currentTime, period);
//            }
//        });
        iv_right_title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(AddDataPage1Activity.class);
            }
        });
    }

    @Override
    public void initData() {
        super.initData();
        getOrg();
    }

    private void requestDateData(String orgId) {
        HttpSender sender = new HttpSender(this, baseUrl, Urls.GET_BAN_CI);
        HashMap<String, String> map = new HashMap<>();
        map.put("orgId", orgId);
        sender.requestPost(map, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {
                Log.e("responedata", e.getMessage());
            }

            @Override
            public void onResponse(String code, String response) {
                Log.e("responedata", response.substring(1, response.length() - 1));
                if (!TextUtils.isEmpty(response)) {
                    Log.e("responedata", response.substring(1, response.length() - 1));
                    List<BanciBean> banciBeans = GsonUtil.jsonToList("[" + response.substring(1, response.length() - 1) + "]", BanciBean.class);
                    final List<String> threeData = new ArrayList<>();
                    for (int i = 0; i < banciBeans.size(); i++) {
                        threeData.add(banciBeans.get(i).getName());
                    }
                    mySpinner03.setmDatas(threeData, new MySpinner.CallBack() {
                        @Override
                        public void onSelect(int postion) {
                            period = threeData.get(postion);
                            requestData(UserManager.getOrgNo(), currentTime, period);
                        }
                    });
                    period = response.substring(1, response.length() - 1);
                    requestData(currentOrgId, currentTime, period);
                }
            }
        });
    }

    @Override
    public void setListener() {
        mAdapter.setOnItemClickListener(new OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(JobEvaluationActivity.this, JobEvaluationInformationActivity.class);
                intent.putExtra(JobEvaluationInformationActivity.JOB_ID, jobEvaluationBeans.get(position).getId());
                startActivity(intent);
            }
        });
        tvStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DatePickerUtil.getInstance(JobEvaluationActivity.this).setCallBack(JobEvaluationActivity.this);
            }
        });
    }

    private void getOrg() {
        HashMap<String, String> hashMap = new HashMap();
//        hashMap.put("objType", "分局,总局");
        hashMap.put("orgId", UserManager.getOrgNo());
        HttpSender sender = new HttpSender(this, baseUrl, Urls.getOrgList);
        sender.requestPost(hashMap, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(final String code, String response) {
                final OrgListBean orgListBean = new Gson().fromJson(response, OrgListBean.class);
                List<String> list = new ArrayList<>();
                for (OrgListBean.ListBean listBean : orgListBean.getList()) {
                    list.add(listBean.getOrgName());
                }
                mySpinner01.setmDatas(list, new MySpinner.CallBack() {
                    @Override
                    public void onSelect(int postion) {
                        currentOrgId = orgListBean.getList().get(postion).getId();
//                        requestData(orgListBean.getList().get(postion).getId(), "", "");
                        requestDateData(currentOrgId);
                    }
                });
                if (orgListBean.getList().size() == 0) return;
                currentOrgId = orgListBean.getList().get(0).getId();
                requestData(orgListBean.getList().get(0).getId(), "", "");
                requestDateData(currentOrgId);

            }
        });
    }


    private void requestData(String orgId, String date, String period) {
        HttpSender sender = new HttpSender(this, baseUrl, Urls.GET_ORG_POST_LIST);
        HashMap<String, String> map = new HashMap<>();
        map.put("orgId", orgId);
        map.put("postDate", date);
        map.put("period", period);
        sender.requestPost(map, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response) {
                JsonParser parser = new JsonParser();
                JsonObject asJsonObject = parser.parse(response).getAsJsonObject();
                JsonArray list = asJsonObject.getAsJsonArray("list");
                jobEvaluationBeans = GsonUtil.jsonToList(list.toString(), JobEvaluationBean.class);
                mAdapter.updateItems(jobEvaluationBeans);
            }
        });
    }

    @Override
    public void onBack(DateBean dateBean) {
        currentTime = dateBean.getmDate();
        tvStart.setText(currentTime);
        requestData(currentOrgId, currentTime, period);
    }


}
